package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.entity.AddressBook;
import com.itheima.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook, HttpServletRequest request) {

        Long userId = (Long) request.getSession().getAttribute("user");
        addressBook.setUserId(userId);
        addressBookService.save(addressBook);
        return R.success("添加收货地址成功");
    }

    @GetMapping("/list")
    public R<List<AddressBook>> getAll(HttpServletRequest request) {
        Long userId = (Long) request.getSession().getAttribute("user");
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(userId != null, AddressBook::getUserId, userId);
        List<AddressBook> list = addressBookService.list(lqw);
        return R.success(list);
    }

    /**
     * 通过id获得当前地址
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> addressFindOne(@PathVariable Long id) {
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(id != null, AddressBook::getId, id);
        AddressBook addressBook = addressBookService.getOne(lqw);
        return R.success(addressBook);
    }

    /**
     * 修改地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook) {
        addressBookService.updateById(addressBook);
        return R.success("修改地址成功");
    }

    @PutMapping("/default")
    public R<String> updateDefault(@RequestBody AddressBook addressBook, HttpServletRequest request) {
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        Long userId = (Long) request.getSession().getAttribute("user");
        lqw.eq(userId != null, AddressBook::getUserId, userId);
        List<AddressBook> list = addressBookService.list(lqw);
        List<AddressBook> collect = list.stream().map(item -> {
            item.setIsDefault(0);
            addressBookService.updateById(item);
            return item;
        }).collect(Collectors.toList());
        lqw.clear();
        lqw.eq(AddressBook::getId, addressBook.getId());
        AddressBook One = addressBookService.getOne(lqw);
        One.setIsDefault(1);
        addressBookService.updateById(One);
        return R.success("设置默认地址成功");
    }
    @DeleteMapping
    public R<String> deleteById(Long id){
        log.info("id为"+id);
        addressBookService.removeById(id);
        return R.success("删除地址成功");
    }
    @GetMapping("/default")
    public R<AddressBook > getDefault(){
        Long userId = BaseContext.getId();
        LambdaQueryWrapper<AddressBook> lqw=new LambdaQueryWrapper<>();
        lqw.eq(userId!=null,AddressBook::getUserId,userId);
        lqw.eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = addressBookService.getOne(lqw);
        return R.success(addressBook);
    }
}
